package org.dreamwork.jasmine2.web.html;

import org.dreamwork.jasmine2.web.controls.WebContainer;
import org.dreamwork.jasmine2.parser.Tag;
import org.dreamwork.jasmine2.events.EventException;
import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-7-21
 * Time: 10:49:38
 */
public class GenericDynamicControl extends WebContainer {

    private static final Logger logger = Logger.getLogger (GenericDynamicControl.class);

//    private Map<String, String> dynamicAttributes = new HashMap<String, String> ();

    public GenericDynamicControl () {}

    public GenericDynamicControl (String tagName) {
        this.htmlTagName = tagName;
    }

    public GenericDynamicControl (Tag tag) {
        htmlTagName = tag.getName ();
        String[] names = tag.getAttributeNames ();
        for (String name : names) {
            String value = tag.getAttribute (name);

            if (value != null) value = value.trim ();
            if (value.charAt (0) == '<' && value.charAt (1) == '%' && value.charAt (2) == '#') {
                setDynamicAttribute (name, value);
            } else {
                setAttribute (name, value);
            }
        }
    }

/*
    public void setDynamicAttribute (String name, String expression) {
        dynamicAttributes.put (name, expression);
    }
*/

    public void onDataBind (Object sender) throws EventException {
        for (String name : dynamicAttributes.keySet ()) {
            String expression = dynamicAttributes.get (name);
            logger.debug ("Name = " + name + ", Expression = " + expression);
/*
            Object value = DataBinder.eval (expression);
            if (value != null)
                setAttribute (name, value.toString ());
*/
        }
    }
}